package com.news.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.news.entity.*;
import com.news.util.JDBCUtil;
/**
 * 用户信息分页类
 * @author 柳胜酉
 *
 */
public class UserPageDao {
	//定义每页显示条数
			public int pageSize=5;
			
			/**
			 * 获取品牌总数量
			 * @return 总数量
			 */
			
			public int getTotal(){
				JDBCUtil util=JDBCUtil.getInstance();
				String sql="select count(1) as total from tb_user";
				ResultSet rs= util.query(sql);
				try {
					if(rs.next()){
						int total= rs.getInt("total");
						return total;
					}
					return 0;
				} catch (Exception e) {
					e.printStackTrace();
					return 0;
				}finally {
					util.close();
				}
			}
			
			/**
			 * 查询当前页品牌列表
			 * @param current 当前页码
			 * @return 品牌列表
			 */
			public List<User> getUsersByCurrentPage(int current){
				JDBCUtil util=JDBCUtil.getInstance();
				try {
					String sql="select * from tb_user order by id DESC limit ?,?";
					List<Object> params=new ArrayList<Object>();
					params.add((current-1)*pageSize);
					params.add(pageSize);
					ResultSet rs= util.query(sql, params);
					List<User> users=new ArrayList<User>();
					while(rs.next()){
						int id = rs.getInt("id");
						String username = rs.getString("username");
						String password = rs.getString("password");
						String role = rs.getString("role");
						int sex = rs.getInt("sex");
						int age = rs.getInt("age");
						User user = new User();
						user.setId(id);
						user.setUsername(username);
						user.setPassword(password);
						user.setRole(role);
						user.setSex(sex);
						user.setAge(age);
						users.add(user);
					}
					return users;
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}finally {
					util.close();
				}
			}
}
